using UnityEditor;
using UnityEngine;

namespace FShaderInspector
{
    /// <summary>
    /// 挑选标签
    /// </summary>
    public class SelectDrawer : FMaterialPropertyDrawer
    {
        public SelectDrawer(string keyword) { this.keyword = keyword; }

        public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
        {
            base.OnGUI(position, prop, label, editor);

            // 获取是否存在不同的折页值
            var hasMixedValue = HasMixedFoldout();

            // 记录GUI默认颜色
            var guiColor = GUI.color;

            if (prop.hasMixedValue)
            {
                // 冲突时正常绘制折页 但关闭折页
                GUI.color = new Color(1, 0.4f, 0.4f, 1);

                var btnRect = FShaderGUI.GetTempRect();
                label = GetFoldoutIcon();
                label.text = Localization.FindName(prop.displayName);
                FShaderGUI.DrawFoldoutGUI(btnRect, label, new Color(0.75f, 0.75f, 0.75f, 1), true);
                SetFoldoutState(false);

                GUI.color = guiColor;
            }
            else
            {
                // 宏启用 绘制折页
                if (IsKeywordEnabled())
                {
                    var btnRect = FShaderGUI.GetTempRect();
                    if (GUI.Button(new Rect(btnRect.x, btnRect.y, btnRect.width - 50, btnRect.height), "", EditorStyles.boldLabel))
                    {
                        if (IsKeywordEnabled() && !prop.hasMixedValue)
                        {
                            if (!hasMixedValue)
                            {
                                foreach (var target in prop.targets)
                                {
                                    var key = EditorCache.instance.GetPropertyKey(target, prop.name);
                                    var state = EditorCache.instance.GetKey(key);
                                    EditorCache.instance.SetKey(key, !state);
                                }
                            }
                            else
                            {
                                // 不同值状态时 默认点击打开
                                foreach (var target in prop.targets)
                                {
                                    var key = EditorCache.instance.GetPropertyKey(target, prop.name);
                                    EditorCache.instance.SetKey(key, true);
                                }
                            }
                        }

                        // 按住alt按键 展开折叠所有子选项
                        if (Event.current.alt)
                            FShaderGUI.SetFoldedChild(prop.name, level, EditorCache.instance.GetKey(labelKey));
                    }

                    label = GetFoldoutIcon(false, hasMixedValue);
                    label.text = Localization.FindName(prop.displayName);
                    FShaderGUI.DrawFoldoutGUI(btnRect, label, new Color(0.75f, 0.75f, 0.75f, 1), true);
                }
                else
                {
                    SetFoldoutState(false);
                }
            }

            // 传递当前页面 level
            FShaderGUI.level = 1;

            // 不同值状态 关闭展开
            // 更新状态到 Editor
            FShaderGUI.isOpen = !hasMixedValue && EditorCache.instance.GetKey(labelKey);
        }
    }
}